<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Crypt;
use DB;

class RegisterController extends Controller
{
    //填写用户基本信息
	
	public function info()
	{
		return view('admin.register.info');
	}

	public function doregister(Request $request)
	{
		// $data = $request -> all();
		// dd($data);
		$this ->validate($request,[
    			'email' => 'required|email',
    			'username' => 'required',
    			'password' => 'required',
    			'repassword' => 'required|same:password',
    		],[
    			'email.required' =>'邮箱不能为空',
    			'email.email' => '邮箱格式不正确',
    			'username.required' => '用户名不能为空',
    			'password.required' => '密码不能为空',
    			'repassword.required' =>'确认密码不能为空',
    			'repassword.same' =>'两次密码不一样',
    			
    		]);

		//处理数据
		$data = $request -> except('_token', 'repassword');
		//加密密码
		$data['password'] = Crypt::encrypt($data['password']);

		$data['remember_token'] = str_random(70);
		$time = date("Y-m-d H:i:s");
		$data['created_at'] = $time;
		$data['updated_at'] = $time;


		// dd($data);
		//添加表
		if($data['auth'] == 0)
		{
			$res = DB::table('admins') -> insert($data);
			if($res)
			{
				return redirect('/admin/login') -> with(['info' => '注册成功']);
			}else
			{
				return redirect('/admin/login') -> with(['info' => '注册失败']);
			}
		}else
		{
			
			$res = DB::table('businesses') -> insert($data);
			if($res)
			{
				return redirect('/admin/login') -> with(['info' => '注册成功']);
			}else
			{
				return redirect('/admin/login') -> with(['info' => '注册失败']);
			}
		}
		
	}

}
